<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="消息类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择消息类型" clearable size="small">
          <el-option label="全部" :value="undefined" />
          <el-option label="个人动态" :value="1" />
          <el-option label="评论/回复" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="用户id" prop="accountId">
        <el-input
          v-model="queryParams.accountId"
          placeholder="请输入用户id"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="发送时间" prop="sendTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.sendTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择发送时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option label="全部" :value="undefined" />
          <el-option label="待审核" :value="1" />
          <el-option label="已通过" :value="2" />
          <el-option label="未通过" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="userActiveMessagingList">
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="发布时间" align="center" prop="sendTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sendTime)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="发布用户" align="center" prop="accountId" />
      <el-table-column label="类型" align="center" prop="type" >
        <template slot-scope="scope">
          <span>{{ scope.row.type===1?'个人动态':'评论/回复'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="内容" align="center" prop="content" :show-overflow-tooltip="true" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.type===1">待审核</span>
          <span v-else-if="scope.row.type===2">已通过</span>
          <span v-else-if="scope.row.type===3">未通过</span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="applyBy" />
      <el-table-column label="审核时间" align="center" prop="applyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime) }}</span>
        </template>
      </el-table-column>
      
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text"  @click="handleUpdate(scope.row)">审核</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:userActiveMessaging:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户动态消息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="内容" prop="content">
          <el-input v-model="form.content" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="消息类型,1:个人动态，2：评论/回复">
          <el-select v-model="form.type" placeholder="请选择消息类型,1:个人动态，2：评论/回复">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="用户id" prop="accountId">
          <el-input v-model="form.accountId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="发送时间" prop="sendTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.sendTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择发送时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="图片消息" prop="photoMessagePath">
          <el-input v-model="form.photoMessagePath" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="评论数" prop="commentCount">
          <el-input v-model="form.commentCount" placeholder="请输入评论数" />
        </el-form-item>
        <el-form-item label="被点赞次数" prop="likeCount">
          <el-input v-model="form.likeCount" placeholder="请输入被点赞次数" />
        </el-form-item>
        <el-form-item label="parent_id存在值时，此纪录为评论消息" prop="parentId">
          <el-input v-model="form.parentId" placeholder="请输入parent_id存在值时，此纪录为评论消息" />
        </el-form-item>
        <el-form-item label="发送动态消息所在地" prop="adress">
          <el-input v-model="form.adress" placeholder="请输入发送动态消息所在地" />
        </el-form-item>
        <el-form-item label="审核时间" prop="applyTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.applyTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择审核时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核人" prop="applyBy">
          <el-input v-model="form.applyBy" placeholder="请输入审核人" />
        </el-form-item>
        <el-form-item label="审核意见" prop="applyText">
          <el-input v-model="form.applyText" placeholder="请输入审核意见" />
        </el-form-item>
        <el-form-item label="状态 1待审核，2已通过，3 未通过">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUserActiveMessaging, getUserActiveMessaging, delUserActiveMessaging, addUserActiveMessaging, updateUserActiveMessaging, exportUserActiveMessaging } from "@/api/system/userActiveMessaging";

export default {
  name: "UserActiveMessaging",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 用户动态消息表格数据
      userActiveMessagingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        content: undefined,
        type: undefined,
        accountId: undefined,
        sendTime: undefined,
        photoMessagePath: undefined,
        commentCount: undefined,
        likeCount: undefined,
        parentId: undefined,
        adress: undefined,
        applyTime: undefined,
        applyBy: undefined,
        applyText: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        type: [
          { required: true, message: "消息类型,1:个人动态，2：评论/回复不能为空", trigger: "blur" }
        ],
        accountId: [
          { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
        sendTime: [
          { required: true, message: "发送时间不能为空", trigger: "blur" }
        ],
        commentCount: [
          { required: true, message: "评论数不能为空", trigger: "blur" }
        ],
        likeCount: [
          { required: true, message: "被点赞次数不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态 1待审核，2已通过，3 未通过不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户动态消息列表 */
    getList() {
      this.loading = true;
      listUserActiveMessaging(this.queryParams).then(response => {
        this.userActiveMessagingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        content: undefined,
        type: undefined,
        accountId: undefined,
        sendTime: undefined,
        photoMessagePath: undefined,
        commentCount: undefined,
        likeCount: undefined,
        parentId: undefined,
        adress: undefined,
        applyTime: undefined,
        applyBy: undefined,
        applyText: undefined,
        status: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户动态消息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUserActiveMessaging(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户动态消息";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateUserActiveMessaging(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addUserActiveMessaging(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除用户动态消息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delUserActiveMessaging(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有用户动态消息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportUserActiveMessaging(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>